package com.flying.cms.service;

import java.util.List;

import com.flying.common.annotation.CommonQuery;
import com.flying.common.annotation.CommonUpdate;
import com.flying.common.image.ImageUtils;
import com.flying.common.log.Logger;
import com.flying.common.util.Utils;
import com.flying.framework.data.Data;
import com.flying.framework.service.AbstractService;

public class AttachmentService  extends AbstractService{
	private final static Logger log = Logger.getLogger(AttachmentService.class);
	
	@CommonUpdate(params={"$SEQ.CMS.ATTACHMENT", "content_id", "name", "tag", "content_type","size","path"})
	public Data create(Data request) throws Exception {
		return request;
	}

	@CommonUpdate(params={"$SEQ.CMS.ATTACHMENT", "content_id", "x-fileName", "tag", "x-contentType","x-size","x-relationPath"})
	public Data save(Data request) throws Exception {
		return request;
	}

	public Data process(Data request) throws Exception {
		final String filepath = request.getString("x-realPath");
		final String press = this.serviceConfig.getConfig("press");
		final boolean isPress = request.getBoolean("press", false);
		if(isPress && press != null && filepath != null && (filepath.toLowerCase().endsWith("jpg") || filepath.toLowerCase().endsWith("jpeg") || filepath.toLowerCase().endsWith("png"))) {
			final float alpha = this.serviceConfig.getConfigs().getFloat("alpha", 0.4f);
			ImageUtils.pressImage(press,filepath,alpha);
			log.debug("processing water press file, press["+ press +"], target file["+filepath+"]");
		}
		return request;
	}
	
	public Data uploads(Data request) throws Exception {
		List<Data> files = request.get("x-files");
		if(files != null)
			for(Data f: files) {
				save(f);
			}
		return request;
	}

	public Data upload(Data request) throws Exception {
		List<Data> files = request.get("x-files");
		if(files != null && !files.isEmpty()) {
			final String up = module.getModuleConfig().getConfig("uploadURL");
			List<String> urls = Utils.newArrayList();
			for(Data f: files) {
				save(f);
				urls.add( up + "/" + f.getString("x-relationPath"));
			}
			return new Data("error", 0, "url", urls.get(0), "urls", urls);
		} else {
			 return new Data("error", 1, "message","no file received!");
		}
	}

	public Data uploadFiles(Data request) throws Exception {
		Data res = new Data();
		for(String key: request.keys()) {
			if(!request.contains(key + ".upload$")) continue;
			Object val = request.get(key);
			if(val instanceof String) {
				res.put(key, module.getModuleConfig().getConfig("uploadURL") + "/" + val);
			} else {
				String[] vals = (String[])val;
				for(int i=0; i < vals.length; i++) {
					vals[i] = module.getModuleConfig().getConfig("uploadURL") + "/" + vals[i];
				}
				res.put(key, vals);
			}
		}
		res.put("error", 0);
		return res;
	}

	@CommonUpdate(params={"name","desc", "tag","attachment_id"})
	public Data update(Data request) throws Exception {
		return request;
	}

	@CommonUpdate(params={"attachment_id"})
	public Data remove(Data request) throws Exception {
		return request;
	}

	@CommonQuery(params={})
	public Data findAll(Data request) throws Exception {
		return request;
	}

	@CommonQuery(params={"tag"})
	public Data findByTag(Data request) throws Exception {
		return request;
	}
}
